package com.demo.order.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.demo.account.api.AccountApi;
import com.demo.account.model.AccountVO;
import com.demo.order.model.auto.Order;
import com.demo.order.mapper.auto.OrderMapper;
import com.demo.order.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wuyijie
 * @since 2021-03-30
 */
@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @DubboReference
    private AccountApi accountApi;


    @Override
    @SentinelResource(value = "orderservice#test1")
    public AccountVO test1(int id) {
        log.info("test1-------------------");
        return accountApi.getById(id);
    }

    @Override
    @SentinelResource(value = "orderservice#test2")
    public AccountVO test2(int id) {
        log.info("test2-------------------");
        return null;
    }

    @Override
    @SentinelResource(value = "orderservice#test3",fallback = "fallbackHandler")
    public AccountVO test3(int id) {
        log.info("test3-------------------");
        return null;
    }


    public AccountVO blockHandler(@RequestParam("id") int id, BlockException exception){
        log.error("blockHandler -----{}",exception.getClass().getCanonicalName());
        return new AccountVO();
    }

    public AccountVO fallbackHandler(@RequestParam("id") int id,BlockException exception){
        log.error("fallbackHandler -----{}",exception.getClass().getCanonicalName());
        return new AccountVO();
    }
}
